Lesson 4 - Operators

You will already be very familiar with operators without really knowing it! Operators will perform a mathematical or logical operation. Like +,- or <, ==. For the most part operators are symbols but they could be short keywords as well. You will be introduced to more operators as the course goes on. At the moment we are only going to focus on the ones you have seen already.

Operator - A symbol / keyword which will perform a specific and well defined operation.

Operators can be split into three main categories. Examples can be seen in the code snippet below.

  1. Mathematical operators
  2. Conditional operators
  3. logical operators

# Example maths operator
x = 8 + 1 - 4
# Example conditional operator
x = 4 > 8
# Example logical operator
x = (1 > 2) and (3 < 4)


There is sometimes some confusion between = and ==. Both of these operators mean different things. The = is known as the assignment operator while == is a conditional operator testing equality. New programmers commonly make the mistake of only using a single = when they are testing equality. The example below shows this mistake and what it should look like.


x = 10
# The next line is using the wrong operator!
if x = 10:
	print "x is 10"
# the next line is the correct way
if x == 10:
	print "x is 10"


Assignment operator - This is specified by a single equals =. This will copy the result of the expression on the right hand side into the variable specified on the left.

Equality operator - This is specified by double equals ==. This will compare the left and right hand sides to see if they are the same.